<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1111111111111111</title>

</head>


<body>
    <script>

        var bluetoothDevice;
        var blserver;
        function addLog(logTXT) {
            var time = new Date().toLocaleTimeString();
            var logString = time + ": " + logTXT;
            $("log").innerHTML += logString + "<br>";
        }

        function onDisconnected(device) {
            if (!bluetoothDevice) {
                return;
            }
            console.log('Disconnecting from Bluetooth Device...');
            if (bluetoothDevice.gatt.connected) {
                bluetoothDevice.gatt.disconnect();
            } else {
                console.log('> Bluetooth Device is already disconnected');
            }
        }

        function connect() {
            var deviceOptions = {
                optionalServices: ['00001f10-0000-1000-8000-00805f9b34fb', '00010203-0405-0607-0809-0a0b0c0d1912', 'ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6', '22210000-554a-4546-5542-46534450464d', 0x180a, 0x181c, 0x181e, 0xfe95, 0x1f10, 0x1f11, 0x181a, 0xfff9, 0xfdcd, 0xffe0, 0xffe1],
                services: [0x180a, 0x181c, 0x181e, 0xfe95, 0x1f10, 0x1f11, 0x181a, 0xfff9, 0xfdcd, 0xffe0, 0xffe1],
                acceptAllDevices: true

            };
            console.log("Searching for devices");
            navigator.bluetooth.requestDevice(deviceOptions).then(device => {
                bluetoothDevice = device;
                device.addEventListener('gattserverdisconnected', onDisconnected);
                console.log(device.name);
                return device.gatt.connect();
            }).then((server) => {
                blserver = server;
                document.getElementById('log').innerText = '蓝牙连接成功\n';
            })
                .catch(onDisconnected);
        }
        function bleReadErr(e) {
            console.log(e);
            document.getElementById('log').innerText = '蓝牙读取错误\n';
        }
        function bleread() {
            var srUUID = parseInt(document.getElementById("serverUUID").value);
            var svUUID = parseInt(document.getElementById("serviceUUID").value);

            if (!bluetoothDevice) {
                document.getElementById('log').innerText = document.getElementById('log').innerText + '无设备连接\n';
            } else {
                blserver.getPrimaryService(srUUID).then(service => service.getCharacteristic(svUUID)).then(characteristic => characteristic.readValue()).then(value => {
                    console.log(value);
                    value = value.buffer ? value : new DataView(value);
                    console.log('Battery percentage:', value.getUint8(0));
                }).catch(bleReadErr)
            }

        }
        function blewrite() {
            if (!bluetoothDevice) {
                document.getElementById('log').innerText = document.getElementById('log').innerText + '无设备连接\n';
            } else {
                blserver.getPrimaryService(0xffe0).then(service => service.getCharacteristic(0xffe3)).then(characteristic => {
                    let resetEnergyExpended = Uint8Array.of(1);
                    characteristic.writeValueWithoutResponse(resetEnergyExpended);
                })
            }
        }

        function getDevInfo() {
            if (!bluetoothDevice) {
                document.getElementById('log').innerText = document.getElementById('log').innerText + '无设备连接\n';
            } else {
                blserver.getPrimaryService(0xffe1).then(service => service.getCharacteristic(0xffe1)).then(characteristic => characteristic.readValue()).then(value => {
                    console.log(value);
                    value = value.buffer ? value : new DataView(value);
                    console.log('0xffe1:', (new Uint8Array(value.buffer)));
                }).catch(bleReadErr)
                blserver.getPrimaryService(0xffe1).then(service => service.getCharacteristic(0xffe2)).then(characteristic => characteristic.readValue()).then(value => {
                    console.log(value);
                    value = value.buffer ? value : new DataView(value);
                    console.log('0xffe2:', (new Uint32Array(value.buffer)));
                    console.log((Math.floor(new Date().getTime() / 1000)).buffer);
                }).catch(bleReadErr)
                blserver.getPrimaryService(0xffe1).then(service => service.getCharacteristic(0xffe3)).then(characteristic => characteristic.readValue()).then(value => {
                    console.log(value);
                    //value = value.buffer ? value : new DataView(value);
                    console.log('0xffe2:', (new Int8Array(value.buffer)));
                }).catch(bleReadErr)
            }

        }
        function a1() {


            const buffer = new ArrayBuffer(4);
            const  dv = new DataView(buffer);
            dv.setUint32(0, (Math.floor(new Date().getTime()/1000)));

            blserver.getPrimaryService(0xffe1).then(service => service.getCharacteristic(0xffe2)).then(characteristic =>
            characteristic.writeValueWithoutResponse(dv))
            .catch(bleReadErr);
            console.log(dv);
        }

    </script>


    <button onclick="connect()">connect</button>
    <button onclick="onDisconnected()">disconnect</button>
    <button onclick="bleread()">read</button>
    <button onclick="blewrite()">write</button>
    <br>
    serverUUID
    <br>
    <input type="text" size="40" id="serverUUID" value="0xffe0">
    <br>
    serviceUUID
    <br>
    <input type="text" size="40" id="serviceUUID" value="0xffe1">
    <div id="log"></div>
    <br>
    <button onclick="getDevInfo()">获取设备信息</button>
    <button onclick="a1()">设置时间</button>
</body>
